VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "c_Array"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Attribute VB_Ext_KEY = "SavedWithClassBuilder" ,"Yes"
Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
'**************************************
'**************************************
'c_ARRAY VERSION 1
'
'By: Jan Botha
'eMail: c03jabot@prg.wcape.school.za
'Using: Visual Basic 5
'Date: 18 September 1999
'**************************************
'**************************************

'Introduction:
'----------------------------------------------------
'I got the idea to program c_Array after I became
'aware of the fact that Collections are slow and
'use quite an amount of memory.
'This class only uses arrays to accomplish everything
'that a collection can do.
'
'I have also added a few things:
'
'1. A MoveUp and MoveDown method to move an item
'   up or down in the array
'2. A Clear method to clear everything
'3. I have added the possibility to use keys to
'   identify an item
'
'Thus, you can use most of the methods by specifying
'either a key or an index
'
'Thank you for using c_Array 1. Please do email
'me on comments, suggestions and especially BUGS!
'Upgrades and improvements coming soon! See the Readme
'file for more information.
'
'The Author
'(-: Jan Botha :-)
'-----------------------------------------------------

Option Explicit

Private Type m_Arrays
    m_Key As String
    m_Value As String
End Type

Private m_Count As Integer 'this will contain the number of items
Private m_Array() As m_Arrays 'Main Array
Private mmm As Collection

Public Function Clear()
    ReDim m_Array(0) As m_Arrays
    m_Count = 0
End Function

Public Function Count() As Integer
    m_Count = UBound(m_Array)
    Count = m_Count
End Function

Public Function Remove(Optional ByVal Index As Integer, Optional ByVal Key As String)
    Dim counter As Integer
    
    'if the key and the index is invalid -> exit function
    If Key = "" And Not Between(Index, 0, m_Count + 1) Then Exit Function
    
    'if the index is invalid, remove item using key
    If Not Between(Index, 0, m_Count + 1) Then
        For counter = 1 To m_Count
            If m_Array(counter).m_Key = Key Then
                RemoveIt counter
                Exit Function
            End If
        Next
      'else if the index is valid
      Else
        RemoveIt Index
    End If
    
End Function

Public Function Add(ByVal Item As String, Optional ByVal Key As String)
    ReDim Preserve m_Array(m_Count + 1) As m_Arrays
    m_Count = m_Count + 1
    'save the key and value
    m_Array(m_Count).m_Value = Item
    m_Array(m_Count).m_Key = Key
End Function

Private Function RemoveIt(ByVal m_Index As Integer)
    Dim tmpArray() As m_Arrays, counter As Integer
    
    'if there is only one item then
    If m_Count = 1 Then
        m_Count = 0
        ReDim m_Array(0) As m_Arrays
        Exit Function
    End If
    
    'otherwise, do the following steps
    ReDim tmpArray(m_Count - 1) As m_Arrays
    
    'save all the values and keys of the items
    'BEFORE Index to the temp. array
    For counter = 1 To m_Index - 1
        tmpArray(counter).m_Key = m_Array(counter).m_Key
        tmpArray(counter).m_Value = m_Array(counter).m_Value
    Next
    
    'save all the values and keys of the items
    'AFTER Index to the temp. array
    For counter = m_Index + 1 To m_Count
        tmpArray(counter - 1).m_Key = m_Array(counter).m_Key
        tmpArray(counter - 1).m_Value = m_Array(counter).m_Value
    Next
    
    'update the m_Count and Redim the main array
    m_Count = m_Count - 1
    ReDim m_Array(m_Count) As m_Arrays
    
    'read all the temp. array's values to the
    'main array
    For counter = 1 To m_Count
        m_Array(counter).m_Key = tmpArray(counter).m_Key
        m_Array(counter).m_Value = tmpArray(counter).m_Value
    Next
    
End Function

Public Function Itemget(Optional ByVal Index As Integer, Optional ByVal Key As String) As String
    Dim counter As Integer
    
    'if the key and index is invalid, exit function
    If Key = "" And Not Between(Index, 0, m_Count + 1) Then Exit Function
    
    'if index is invalid, get item value using
    'the key
    If Not Between(Index, 0, m_Count + 1) Then
        For counter = 1 To m_Count
            'check to see if this is the item that's needed
            If m_Array(counter).m_Key = Key Then
                Itemget = m_Array(counter).m_Value
                Exit Function
            End If
        Next
      'else if the index is valid, get the value
      'using the index
      Else
        Itemget = m_Array(Index).m_Value
    End If

End Function

Public Function Itemset(ByVal sValue As String, Optional ByVal Index As Integer, Optional ByVal Key As String)
    Dim counter As Integer
    
    'if the key and index is invalid, exit function
    If Key = "" And Not Between(Index, 0, m_Count + 1) Then Exit Function
    
    'if the index is invalid, set the item value
    'using the key
    If Not Between(Index, 0, m_Count + 1) Then
        For counter = 1 To m_Count
            'check if this is the item which value has to be changed
            If m_Array(counter).m_Key = Key Then
                m_Array(counter).m_Value = sValue
                Exit Function
            End If
        Next
      'otherwise if the index is valid, use it
      Else
        m_Array(Index).m_Value = sValue
    End If

End Function

Public Function MoveUp(Optional ByVal Index As Integer, Optional ByVal Key As String)
    Dim counter As Integer
    'if the key and index is invalid, exit function
    If Key = "" And Not Between(Index, 0, m_Count + 1) Then Exit Function
    
    'if index is invalid, use the key
    If Not Between(Index, 0, m_Count + 1) Then
        For counter = 1 To m_Count
            'check if this is the item to move up
            If m_Array(counter).m_Key = Key Then
                MoveItUp counter
                Exit Function
            End If
        Next
     'otherwise, move the item using its index
      Else
        MoveItUp Index
    End If
End Function

Private Function MoveItUp(ByVal Index As Integer)
    Dim tmpContain As m_Arrays
    
    'since you cannot move the topmost item further
    'up, the function exits
    'the topmost item's index will be 0
    If Index = 1 Then Exit Function
    
    'store the key and value of the item above the
    'about-to-bo-moved item in a temp. container
    tmpContain.m_Key = m_Array(Index - 1).m_Key
    tmpContain.m_Value = m_Array(Index - 1).m_Value
    
    'store the key and value of the item
    'about-to-be-move item to the item abot the
    'about-to-be-moved item
    m_Array(Index - 1).m_Key = m_Array(Index).m_Key
    m_Array(Index - 1).m_Value = m_Array(Index).m_Value
    
    'restore the temp. key and value to the item that
    'is now below the moved-item
    m_Array(Index).m_Key = tmpContain.m_Key
    m_Array(Index).m_Value = tmpContain.m_Value
    
End Function

Public Function MoveDown(Optional ByVal Index As Integer, Optional ByVal Key As String)
    Dim counter As Integer
    'if the key and index is invalid, exit function
    If Key = "" And Not Between(Index, 0, m_Count + 1) Then Exit Function
    
    'if index is invalid, move using key
    If Not Between(Index, 0, m_Count + 1) Then
        For counter = 1 To m_Count
            'check if this is the item that has to be moved
            If m_Array(counter).m_Key = Key Then
                MoveItDown counter
                Exit Function
            End If
        Next
      'otherwise, if the index is not invalid, USE IT
      Else
        MoveItDown Index
    End If
End Function

Private Function MoveItDown(ByVal Index As Integer)
    Dim tmpContain As m_Arrays
    
    'since you cannot move the bottommost item further
    'down, the function exits
    'the bottommost item's index will be equal to m_Count
    If Index = m_Count Then Exit Function
    
    'store the key and value of the item below the
    'about-to-bo-moved item in a temp. container
    tmpContain.m_Key = m_Array(Index + 1).m_Key
    tmpContain.m_Value = m_Array(Index + 1).m_Value
    
    'store the key and value of the item
    'about-to-be-move item to the item above the
    'about-to-be-moved item
    m_Array(Index + 1).m_Key = m_Array(Index).m_Key
    m_Array(Index + 1).m_Value = m_Array(Index).m_Value
    
    'store the temp. key and value to the item that
    'is now obove the moved-item
    m_Array(Index).m_Key = tmpContain.m_Key
    m_Array(Index).m_Value = tmpContain.m_Value
End Function

Private Function Between(ByVal Number As Long, Min As Long, Max As Long) As Boolean
    'I use this function to determine quickly and easily if
    'one value is between to others
    If Number > Min And Number < Max Then Between = True Else Between = False
End Function
